<?php
/**
 * 操作日志记录
 */
namespace app\common\model\tag;
use think\Model;

class Question extends Model {

    protected $table = 'question';
    protected $connection = ['database' => 'jooj_tag'];
    
    public function setTrueTable($companyId){
        $this->table = $this->table."_".$companyId;
    }

    /**
     * 获取当前模型的多条记录
     * 
     * @params type $field  所查的参数（column的筛选）
     * @param type $where  条件
     * @params type $order 排序字符串
     * @params number limit 多少条
     * @return type $list  list列表
     */
    public function getList($field = '*', $where = [], $order = '', $limit = 0) {

        $list = $this->setTable($this->table)->field($field);

        if ($where) {
            $list = $list->where($where);
        }

        if ($order) {
            $list = $list->order($order);
        }

        if ($limit) {
            $list = $list->limit($limit);
        }

        $list = $list->select();

        return $list;
    }

    /**
     * 获取当前列表的单条记录
     * 
     * @param type $field 获取的列名，column name 以，号分割
     * @param type $where 条件
     * @return array $array
     */
    public function getOne($field = '*', $where = []) {
        return $this->setTable($this->table)->field($field)->where($where)->find();
    }


    /**
     * 根据条件获取多条数据
     * @params string $field 查询的字段
     * @params array  $condition 查询条件
     */
    public function getLists($condition = [], $field = "*") {
        return $this->setTable($this->table)->field($field)->where($condition)->select();
    }
    
    /**
     * 添加
     * @param type $data
     */
    public function addOne($data=[]){
        $iData = [];
        
        isset($data['mobile'])?$iData['mobile'] = $data['mobile']:'';
        isset($data['birthday'])?$iData['birthday'] = $data['birthday']:'';
        isset($data['agree_1'])?$iData['agree_1'] = $data['agree_1']:'';
        isset($data['agree_2'])?$iData['agree_2'] = $data['agree_2']:'';
        isset($data['agree_3'])?$iData['agree_3'] = $data['agree_3']:'';
        isset($data['agree_4'])?$iData['agree_4'] = $data['agree_4']:'';
        isset($data['agree_5'])?$iData['agree_5'] = $data['agree_5']:'';
        isset($data['agree_6'])?$iData['agree_6'] = $data['agree_6']:'';
        isset($data['agree_7'])?$iData['agree_7'] = $data['agree_7']:'';
        isset($data['agree_8'])?$iData['agree_8'] = $data['agree_8']:'';
        isset($data['add_ts'])?$iData['add_ts'] = $data['add_ts']:'';
        isset($data['come_from'])?$iData['come_from'] = $data['come_from']:'';
        isset($data['gender'])?$iData['gender'] = $data['gender']:'';
        isset($data['vip_id'])?$iData['vip_id'] = $data['vip_id']:'';
        isset($data['come_ts'])?$iData['come_ts'] = $data['come_ts']:'';
        isset($data['acrm_back'])?$iData['acrm_back'] = $data['acrm_back']:'';
        
        return $this->setTable($this->table)->insertGetId($iData);
    }
    
    
    /**
     * 
     * @param type $data
     * @return type
     */
    public function updateOneByQuestionId($data){
        $iData = [];
        
        isset($data['mobile'])?$iData['mobile'] = $data['mobile']:'';
        isset($data['birthday'])?$iData['birthday'] = $data['birthday']:'';
        isset($data['agree_1'])?$iData['agree_1'] = $data['agree_1']:'';
        isset($data['agree_2'])?$iData['agree_2'] = $data['agree_2']:'';
        isset($data['agree_3'])?$iData['agree_3'] = $data['agree_3']:'';
        isset($data['agree_4'])?$iData['agree_4'] = $data['agree_4']:'';
        isset($data['agree_5'])?$iData['agree_5'] = $data['agree_5']:'';
        isset($data['agree_6'])?$iData['agree_6'] = $data['agree_6']:'';
        isset($data['agree_7'])?$iData['agree_7'] = $data['agree_7']:'';
        isset($data['agree_8'])?$iData['agree_8'] = $data['agree_8']:'';
        isset($data['add_ts'])?$iData['add_ts'] = $data['add_ts']:'';
        isset($data['come_from'])?$iData['come_from'] = $data['come_from']:'';
        isset($data['gender'])?$iData['gender'] = $data['gender']:'';
        isset($data['vip_id'])?$iData['vip_id'] = $data['vip_id']:'';
        isset($data['come_ts'])?$iData['come_ts'] = $data['come_ts']:'';
        isset($data['acrm_back'])?$iData['acrm_back'] = $data['acrm_back']:'';
        
        return $this->setTable($this->table)->where(['id'=>"{$data['question_id']}"])->update($iData);
    }
}
